//
//  PersonalHomePageVideoController.m
//  OnionHome
//
//  Created by fengjie on 2019/9/27.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "PersonalHomePageVideoController.h"
#import "PersonalHomePageVideoCell.h"
#import "CLPlayerView.h"

@interface PersonalHomePageVideoController ()<UITableViewDelegate,UITableViewDataSource,CLTableViewCellDelegate, UIScrollViewDelegate>
/**listView*/
@property (nonatomic,strong)UITableView*listView;
@property (nonatomic , strong)NSMutableArray *dataArray;

/**CLplayer*/
@property (nonatomic, weak) CLPlayerView *playerView;
/**记录Cell*/
@property (nonatomic, assign) UITableViewCell *cell;
@end

@implementation PersonalHomePageVideoController

- (void)loadView{
    [super loadView];
    [self.view addSubview:self.listView];
    [self.listView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    //把滚动视图交给管理器
    if (self.sManager) {
        [self.sManager addChildView:self.listView];
    }

}

- (void)viewDidLoad {
    [super viewDidLoad];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self pausePlay];
}
#pragma mark --UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 8.0f;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01f;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return UIView.new;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return UIView.new;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PersonalHomePageVideoCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([PersonalHomePageVideoCell class]) forIndexPath:indexPath];
    cell.delegate = self;
    kWeakSelf(weakSelf);
    cell.tapMessageBlock = ^{
        [weakSelf pushVideoDetail:weakSelf.dataArray[indexPath.section]];
    };
    return cell;
}

#pragma mark - UITableViewDelegate
//在willDisplayCell里面处理数据能优化tableview的滑动流畅性，cell将要出现的时候调用
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    PersonalHomePageVideoCell * myCell = (PersonalHomePageVideoCell *)cell;
    myCell.model = self.dataArray[indexPath.section];
}
//cell离开tableView时调用
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    //因为复用，同一个cell可能会走多次
    if ([_cell isEqual:cell]) {
        //区分是否是播放器所在cell,销毁时将指针置空
        [_playerView destroyPlayer];
        _cell = nil;
    }
}
#pragma mark - 点击播放代理
- (void)cl_tableViewCellPlayVideoWithCell:(PersonalHomePageVideoCell *)cell{
    //记录被点击的Cell
    _cell = cell;
    //销毁播放器
    [_playerView destroyPlayer];
    CLPlayerView *playerView = [[CLPlayerView alloc] initWithFrame:CGRectZero];
    _playerView = playerView;
    [cell.playerBgView addSubview:_playerView];
    [playerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    //    //重复播放，默认不播放
    //    _playerView.repeatPlay = YES;
    //    //当前控制器是否支持旋转，当前页面支持旋转的时候需要设置，告知播放器
//        _playerView.isLandscape = YES;
    //    //设置等比例全屏拉伸，多余部分会被剪切
    //    _playerView.fillMode = ResizeAspectFill;
    //设置进度条背景颜色
    _playerView.progressBackgroundColor = [UIColor colorWithRed:53 / 255.0 green:53 / 255.0 blue:65 / 255.0 alpha:1];
    //设置进度条缓冲颜色
    _playerView.progressBufferColor = [UIColor grayColor];
    //设置进度条播放完成颜色
    _playerView.progressPlayFinishColor = UIColorFromRGB(0x009966);
    //    //全屏是否隐藏状态栏
//        _playerView.fullStatusBarHidden = NO;
    //    //转子颜色
    //    _playerView.strokeColor = [UIColor redColor];
    //视频地址
    _playerView.url = [NSURL URLWithString:cell.model.videoUrl];
    //播放
    [_playerView playVideo];
    //返回按钮点击事件回调
    [_playerView destroyPlay:^{
        cell.stopPlay = YES;
        DLog(@"播放器被销毁了");
    }];
    [_playerView backButton:^(UIButton *button) {
        DLog(@"返回按钮被点击");
    }];
    //播放完成回调
    kWeakSelf(weakSelf);
    [_playerView endPlay:^{
        //销毁播放器
        [weakSelf.playerView destroyPlayer];
        weakSelf.playerView = nil;
        weakSelf.cell = nil;
        DLog(@"播放完成");
    }];
}

/**暂停*/
- (void)pausePlay{
    [self.playerView pausePlay];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self pushVideoDetail:self.dataArray[indexPath.section]];
}
-(void)pushVideoDetail:(PersonalHomePageModel *)model{
    
    VideoDetailController * vc = [VideoDetailController new];
    PlayModel * playmodel = [[PlayModel alloc] init];
    playmodel.videoUrl = model.videoUrl;
    playmodel.userImage = IMAGE_NAMED(model.userImage);
    playmodel.pictureUrl = model.pictureUrl;
    vc.model = playmodel;
    
    [self.navigationController pushViewController:vc animated:YES];
}
-(UITableView*)listView{
    if(!_listView){
        _listView=[[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _listView.delegate = self;
        _listView.dataSource = self;
        _listView.rowHeight = UITableViewAutomaticDimension;
        _listView.estimatedRowHeight = 44.0f;

        _listView.tableFooterView = UIView.new;
        [_listView registerClass:[PersonalHomePageVideoCell class] forCellReuseIdentifier:NSStringFromClass([PersonalHomePageVideoCell class])];
    }
    return _listView;
}

-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
        NSData *JSONData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Date" ofType:@"json"]];
        NSArray *array = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingAllowFragments error:nil];
        [array enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            PersonalHomePageModel *model = [PersonalHomePageModel new];
            [model setValuesForKeysWithDictionary:obj];
            [_dataArray addObject:model];
        }];
    }
    return _dataArray;
}
@end
